package com.btr.ivle.helper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.btr.ivle.PostForumActivity;
import com.btr.ivle.R;

public class ForumPostListAdapter extends ArrayAdapter<Map<String,String>> {
	
	private Context context;
	private Activity myApp;
	
	public final static String MSG_ID = ".ViewForumPostActivity.Msg.Id";
	public final static String MSG_TITLE = ".ViewForumPostActivity.Msg.Title";
	public final static String MSG_REPLY = ".ViewForumPostActivity.Msg.Reply";
	
    public ForumPostListAdapter(Context context, List<Map<String, String>> list, Activity myApp) {
        super(context, 0, list);
        this.context = context;
        this.myApp = myApp;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.parseLong(getItem(position).get("PostDate")));
        Date date = calendar.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
        
    	View view = convertView;
    	view = LayoutInflater.from(getContext()).inflate(R.layout.forumlist, null);
    	
    	TextView tvTitle = (TextView) view.findViewById(R.id.PostTitle);
    	tvTitle.setText(getItem(position).get("PostTitle"));
    	
    	TextView tvBody = (TextView) view.findViewById(R.id.PostBody);
    	tvBody.setText(Html.fromHtml(getItem(position).get("PostBody")));
    	
    	TextView tvName = (TextView) view.findViewById(R.id.PosterName);
    	tvName.setText(getItem(position).get("PosterName"));
    	
    	TextView tvTime = (TextView) view.findViewById(R.id.PostTime);
    	tvTime.setText(dateFormat.format(date));
    	if(getItem(position).containsKey("isPosterStaff")){
    		tvName.setTextColor(context.getResources().getColor(R.color.dark_green));
    	}
    	
    	ImageView iv = (ImageView) view.findViewById(R.id.imageViewReply);
    	iv.setTag(position);
    	iv.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
            	ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
            	adapter.add("Reply");
            	adapter.add("Reply/Quote");
            	adapter.add("Email Poster");
            	AlertDialog.Builder builder = new AlertDialog.Builder(context);
            	//builder.setTitle("");
            	final int pos = (Integer) v.getTag();
            	builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            	    public void onClick(DialogInterface dialog, int item) {
            	    	Intent intent;
            	    	
            	    	switch(item){
            	    	case 0:
            	    		intent = new Intent(context, PostForumActivity.class);
            		        intent.putExtra(MSG_ID, getItem(pos).get("ID"));
            		        intent.putExtra(MSG_TITLE, "Re: " + getItem(pos).get("PostTitle"));
            		        //context.startActivity(intent);
            		        myApp.startActivityForResult(intent, 0);
            	    		break;
            	    	case 1:
            	    		String text = "<br /><br />------------------------------<br />" + getItem(pos).get("PostBody");
            	    		intent = new Intent(context, PostForumActivity.class);
            		        intent.putExtra(MSG_ID, getItem(pos).get("ID"));
            		        intent.putExtra(MSG_TITLE, "Re: " + getItem(pos).get("PostTitle"));
            		        intent.putExtra(MSG_REPLY, Html.fromHtml(text).toString());
            		        myApp.startActivityForResult(intent, 0);
            	    		break;
            	    	case 2:
            	    		String email = getItem(pos).get("PosterEmail");
            	    		String emailSubject = "Re: " + getItem(pos).get("PostTitle");
            	    		String emailText = "<br /><br />------------------------------<br />" + getItem(pos).get("PostBody");

        	    		    String emailAddressList[] = {email};
        	    		    intent = new Intent((android.content.Intent.ACTION_SEND)); 
        	    		   	intent.setType("text/html");
        	    		   	intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);  
        	    		   	intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject); 
        	    		   	intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(emailText)); 
        	    		   	context.startActivity(intent);
        	    		   	//myApp.startActivityForResult(intent, 0);
        	    		   	break;
            	    	}
            	    }
            	});
            	AlertDialog alert = builder.create();
            	alert.show();
            }
        });
        return view;
    }
}
